package com.solotiger.douban.util;


import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.trello.rxlifecycle2.components.RxActivity;
import com.trello.rxlifecycle2.components.support.RxFragment;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle2.components.support.RxDialogFragment;

import org.reactivestreams.Subscription;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName Android_phone_app
 * @Author: linqiang
 * @Version: 1.0.0
 * @ClassName: RxHelper
 * @JDK <JDK1.8>
 * Description: RxJava一些封装类
 */

public final class RxHelper {
    /**
     * 在IO线程中生产在UI线程中使用
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> bind() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> bind(final RxActivity activity) {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(RxLifecycle.bindUntilEvent(activity.lifecycle(), ActivityEvent.DESTROY));
            }
        };
    }

    /**
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> bind(final RxAppCompatActivity activity) {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(RxLifecycle.bindUntilEvent(activity.lifecycle(), ActivityEvent.DESTROY));
            }
        };
    }

    /**
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> bind(final RxFragment fragment) {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(RxLifecycle.bindUntilEvent(fragment.lifecycle(), FragmentEvent.DESTROY));
            }
        };
    }

    /**
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> bind(final RxDialogFragment fragment) {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(RxLifecycle.bindUntilEvent(fragment.lifecycle(), FragmentEvent.DESTROY));
            }
        };
    }

    public static void unsubscribeIfNotNull(Subscription subscription) {
        if (subscription != null) {
            subscription.cancel();
        }
    }

    public static CompositeDisposable getNewCompositeSubIfUnsubscribed(CompositeDisposable
                                                                               subscription) {
        if (subscription == null || subscription.isDisposed()) {
            return new CompositeDisposable();
        }
        return subscription;
    }
}
